from rest_framework import request
from rest_framework.generics import ListAPIView
from rest_framework import generics
from rest_framework.filters import OrderingFilter
from goods.models import SKU
from goods.serializers import HotSKUSerializer, SKUSerializer


#商品分类后端接口  GET /goods/categories/(?P<category_id>\d+)/skus/?page=xxx&page_size=xxx&ordering=xxx
class SKUListView(ListAPIView):
    serializer_class = SKUSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time', 'price', 'sales')
    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return  SKU.objects.filter(category = category_id,is_launched = True)



#热销商品后端   GET /goods/categories/(?P<category_id>\d+)/hotskus/
class HotSKUListView(ListAPIView):
    serializer_class = HotSKUSerializer

    def get_queryset(self):
        category_id  = self.kwargs['category_id']   #用正常查询字符串方式也可以获取category_id
        hotSKU = SKU.objects.filter(category = category_id,is_launched=True).order_by('-sales')[0:2]
        return hotSKU